package org.tagging.service.impl.other;

import org.springframework.stereotype.Service;
import org.tagging.dao.TaggingUserAuthorityMapper;
import org.tagging.model.TaggingUserAuthority;
import org.tagging.model.TaggingUserAuthorityExample;
import org.tagging.utils.StatusCode;

import javax.annotation.Resource;
import java.util.List;

@Service
public class AuthorityUserServiceImpl {

    @Resource
    private TaggingUserAuthorityMapper userAuthorityMapper;

    public List<TaggingUserAuthority> queryByUserId(Integer userId, TaggingUserAuthority.Column... columns) {
        TaggingUserAuthorityExample example = new TaggingUserAuthorityExample();
        TaggingUserAuthorityExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        return userAuthorityMapper.selectByExampleSelective(example, columns);
    }

    public List<TaggingUserAuthority> queryTemplate() {
        TaggingUserAuthorityExample example = new TaggingUserAuthorityExample();
        TaggingUserAuthorityExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(StatusCode.AUTHORITY_TEMPLATE);
        return userAuthorityMapper.selectByExampleSelective(example);
    }

    public void removeAuthorityUser(String authorityName, Integer userId) {
        TaggingUserAuthorityExample example = new TaggingUserAuthorityExample();
        TaggingUserAuthorityExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId);
        criteria.andAuthorityEqualTo(authorityName);
        userAuthorityMapper.deleteByExample(example);
    }

    public void addAuthorityUser(String authorityName, Integer userId) {
        TaggingUserAuthority userAuthority = new TaggingUserAuthority();
        userAuthority.setUserId(userId);
        userAuthority.setAuthority(authorityName);
        userAuthorityMapper.insertSelective(userAuthority);
    }
}
